from textblob import TextBlob
from vocab_type_table import VOCAB_SPEECH_TYPES
from time_machine_data import read_time_machine

class Vocab(object):
    def __init__(self,text):
        self.vocab_speech_types = VOCAB_SPEECH_TYPES
        self.origin_text = text
        self.vocab_sentences = self.get_sentences_list()
        # 英文词性符号列表，增加一个未知的词为 SPEECHUNK
        self.vocab_speech_type_list = [key for key in self.vocab_speech_types]+["SPEECHUNK"]
    def get_sentences_list(self):
        """
        根据提供的句子获取到单词和词性的列表
        """
        text = self.origin_text
        blob = TextBlob(text)
        sentences = blob.sentences
        vocab_sentences = []
        for sentence in sentences:
            vocab_list = []
            # print("sentence",sentence)
            blob = TextBlob(str(sentence))
            for word, pos in blob.tags:
                vocab_list.append((word, pos,self.vocab_speech_types[pos] if pos in self.vocab_speech_types else "SPEECHUNK"))
            vocab_sentences.append(vocab_list)
        return vocab_sentences

sentence = read_time_machine()
vocab = Vocab(sentence)
print(vocab.origin_text)
print(len(vocab.vocab_sentences))
for sentence in vocab.vocab_sentences:
    print("####################")
    print([word[0] for word in sentence])
    print([word[1] for word in sentence])
    print([word[2] for word in sentence])
